/**
 * 自定义函数
 * */


//列表
function getList(){
    var uinfo = getUserInfo();
    var formData = {
        uid: uinfo.uid, role_id: uinfo.role_id, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//拼接字符串
function getHtml(d){
    var h = '', i = 0;
    for (; i < d.length; i ++) {
        h += '<table class="tbox" cellspacing="0">';
        h += '<tr>';
        h += '<td class="tdl">人员名称</td>';
        h += '<td class="tdr">' + d[i].uname + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">所在部门</td>';
        h += '<td class="tdr">' + d[i].depart_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">身份</td>';
        h += '<td class="tdr">' + d[i].lead_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">角色</td>';
        h += '<td class="tdr">' + d[i].role_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">账号状态</td>';
        h += '<td class="tdr">' + d[i].status_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl"></td>';
        h += '<td class="tdr">';
        h += '<a href="javascript:;" onclick="getOne(' + d[i].uid + ');">修改</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="editPwd(' + d[i].uid + ');">修改密码</a>';
        // h += '<input type="hidden" id="status' + d[i].uid + '" value="' + d[i].status + '"/>';
        h += '</td>';
        h += '</tr>';
        h += '</table>';
    }
    $('#tbody').html(h);
}


// 获取详情
function getOne(v){
    // var uinfo = getUserInfo();
    var formData = {
        id: v,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            editHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//编辑弹窗
function editHtml(e){
    var d = e.data;
    var depart = e.depart, lead = e.lead, role = e.role, able = e.able;
    var h = '', i;
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt" style="height: 70vh; top: 10vh;">';
    h += '<div>员工名称</div>';
    h += '<input type="text" id="uname" placeholder="输入员工姓名" value="' + d.title + '"/>';
    h += '<div>部门</div>';
    h += '<select id="depart_id">';
    if (d.depart_id == 0) {
        h += '<option value="0" selected>-选择-</option>';
    } else {
        h += '<option value="0">-选择-</option>';
    }
    for (i = 0; i < depart.length; i ++) {
        if (d.depart_id == depart[i].id) {
            h += '<option value="' + depart[i].id + '" selected>';
        } else {
            h += '<option value="' + depart[i].id + '">';
        }
        h += depart[i].title;
        h += '</option>';
    }
    h += '</select>';
    h += '<div>身份</div>';
    h += '<select id="depart_lead">';
    for (i = 0; i < lead.length; i ++) {
        if (d.depart_lead == lead[i].id) {
            h += '<option value="' + lead[i].id + '" selected>';
        } else {
            h += '<option value="' + lead[i].id + '">';
        }
        h += lead[i].title;
        h += '</option>';
    }
    h += '</select>';
    h += '<div>角色</div>';
    h += '<select id="role_id">';
    if (d.role_id == 0) {
        h += '<option value="0" selected>-选择-</option>';
    } else {
        h += '<option value="0">-选择-</option>';
    }
    for (i = 0; i < role.length; i ++) {
        if (d.role_id == role[i].id) {
            h += '<option value="' + role[i].id + '" selected>';
        } else {
            h += '<option value="' + role[i].id + '">';
        }
        h += role[i].title;
        h += '</option>';
    }
    h += '</select>';
    h += '<div>状态设置</div>';
    h += '<select id="is_able">';
    for (i = 0; i < able.length; i ++) {
        if (d.is_able == able[i].id) {
            h += '<option value="' + able[i].id + '" selected>';
        } else {
            h += '<option value="' + able[i].id + '">';
        }
        h += able[i].title;
        h += '</option>';
    }
    h += '</select>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="modify(' + d.id + ');">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


// //编辑弹窗
// function edit(v){
//     var status = $('#status' + v).val();
//     var h = '';
//     h += '<div class="popMask"></div>';
//     h += '<div class="popCnt">';
//     h += '<div>状态设置</div>';
//     h += '<select id="is_able">';
//     if (status == 0) {
//         h += '<option value="0" selected>正常</option>';
//         h += '<option value="1">禁用</option>';
//     } else {
//         h += '<option value="0">正常</option>';
//         h += '<option value="1" selected>禁用</option>';
//     }
//     h += '</select>';
//     h += '<div class="popBtn">';
//     h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
//     h += '<button onclick="modify(' + v + ');">保存</button>';
//     h += '</div>';
//     h += '</div>';
//     $('#formPop').html(h);
// }


//编辑保存
function modify(v){
    var uname = $('#uname').val();
    var depart_id = $('#depart_id').val();
    var depart_lead = $('#depart_lead').val();
    var role_id = $('#role_id').val();
    var is_able = $('#is_able').val();
    var formData = {
        title: uname, depart_id: depart_id, 
        depart_lead: depart_lead, role_id: role_id,
        uid: v, is_able: is_able,
    };
    //console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.href = '';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//编辑吗密码弹窗
function editPwd(v){
    var h = '';
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt" style="height: 50vh; top: 15vh;">';
    h += '<div>原始密码</div>';
    h += '<input type="passward" id="pwd" placeholder="输入原密码"/>';
    h += '<div>新密码</div>';
    h += '<input type="password" id="pwdnew" placeholder="输入新密码"/>';
    h += '<div>再次新密码</div>';
    h += '<input type="password" id="pwdnew2" placeholder="输入新密码"/>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="setPwd(' + v + ');">马上更新</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


//密码保存
function setPwd(v){
    var pwd = $('#pwd').val();
    var pwdnew = $('#pwdnew').val();
    var pwdnew2 = $('#pwdnew2').val();
    if (pwdnew != pwdnew2) {
        msgPop({msg: '两次新密码输入不一致'}); return
    }
    var formData = {
        id: v, pwd: pwd, pwdnew: pwdnew,
    };
    //console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/passwd-set',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}